<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class UserLogin extends CFormModel
{
	public $email;
	public $ticket_code;
	//public $rememberMe;

	/**
	 * Declares the validation rules.
	 * The rules state that email and ticket_code are required,
	 * and ticket_code needs to be authenticated.
	 */
	public function rules()
	{
		return array(
			// email and ticket_code are required
			array('email, ticket_code', 'required'),
			// rememberMe needs to be a boolean
			//array('rememberMe', 'boolean'),
			// ticket_code needs to be authenticated
			//array('ticket_code', 'authenticate'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels()
	{
		return array(
			//'rememberMe'=>TicketsModule::t("Remember me next time"),
			'email'=>'Email',
			'ticket_code'=>'Ticket Code',
		);
	}

	/**
	 * Authenticates the ticket_code.
	 * This is the 'authenticate' validator as declared in rules().
	 */
	public function authenticate($attribute,$params)
	{
		if(!$this->hasErrors())  // we only want to authenticate when no input errors
		{
			$identity=new UserIdentity($this->email,$this->ticket_code);
			$identity->authenticate();
			switch($identity->errorCode)
			{
				
				case UserIdentity::ERROR_email_INVALID:
					$this->addError("email",TicketsModule::t("email is incorrect."));
					break;
				
				case UserIdentity::ERROR_ticket_code_INVALID:
					$this->addError("ticket_code",TicketsModule::t("TicKet is incorrect."));
					break;
			}
		}
	}
}
